package com.yinxin.util;

import java.sql.*;

/**
 * @Author: YinXin
 * @Date: 2021/10/12 9:17
 * @Version: 1.0
 * @description:
 */
public class JDBCUtils {
    private JDBCUtils(){}

    private static Connection con;

    static{
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url= "jdbc:mysql://localhost:3306/kflb?unicode=true&characterEncode=utf8&serverTimezone=GMT%2B8";
            String username = "root";
            String password = "root";
            con = DriverManager.getConnection(url, username, password);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


    /**
     * 获取连接的方法
     * @return
     */
    public static Connection getConnection(){
        return con;
    }


    /**
     * 关闭资源
     * @param con
     * @param stat
     */
    public static void close(Connection con, Statement stat){
        if (con!=null) {
            try {
                con.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        if (stat!=null) {
            try {
                stat.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }


    /**
     * 关闭资源
     * @param con
     * @param pst
     * @param rs
     */
    public static void close(Connection con, PreparedStatement pst, ResultSet rs){
        if (con!=null) {
            try {
                con.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        if (pst!=null) {
            try {
                pst.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        if (rs!=null) {
            try {
                rs.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }
}
